package com.atguigu.gulimall.member.service.impl;

import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneException;
import com.atguigu.gulimall.member.exception.UsernameException;
import com.atguigu.gulimall.member.vo.MemberUserRegisterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberUserRegisterVo vo) {
        MemberLevelEntity levelEntity = memberLevelDao.selectOne(new LambdaQueryWrapper<MemberLevelEntity>().eq(MemberLevelEntity::getDefaultStatus, 1));

        String userName = vo.getUserName();
        String phone = vo.getPhone();
        if (userName == null || StringUtils.isBlank(userName)) {
            throw new UsernameException("请传入用户名");
        }
        this.checkUserNameUnique(userName);
        if (phone == null || StringUtils.isBlank(phone)) {
            throw new UsernameException("请传入手机号码");
        }
        this.checkPhoneUnique(phone);
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setLevelId(levelEntity.getId());
        memberEntity.setUsername(userName);
        memberEntity.setNickname(vo.getUserName());
        memberEntity.setMobile(phone);
        memberEntity.setGender(0);
        memberEntity.setCreateTime(new Date());

        // 对密码进行md5加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);
        this.baseMapper.insert(memberEntity);
    }

    @Override
    public void checkUserNameUnique(String userName) {
        Integer userCount = memberDao.checkUserExist(userName);
        if (userCount > 0) {
            throw new UsernameException();
        }
    }

    @Override
    public void checkPhoneUnique(String mobile) {
        int count = this.count(new LambdaQueryWrapper<MemberEntity>().eq(MemberEntity::getMobile, mobile));
        if (count > 0) {
            throw new PhoneException();
        }
    }

}